﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;
using WuDada.Core.Generic.Util;

public partial class member_rent : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private ConfigHelper m_ConfigHelper = new ConfigHelper();
    private SessionHelper m_SessionHelper = new SessionHelper();
    private ITagService m_TagService;

    //熱門案件 NodeId=8
    private int m_NodeId = 8;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_TagService = m_PostFactory.GetTagService();

        if (!IsPostBack)
        {
            if (m_SessionHelper.Member != null)
            {
                FillRepeater();
            }
        }
    }

    private void FillRepeater()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", m_NodeId.ToString());
        conditions.Add("CreateMember", m_SessionHelper.Member.MemberId.ToString());

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;
        conditions.Add("PageIndex", pageIndex.ToString());
        conditions.Add("PageSize", pageSize.ToString());

        Repeater1.DataSource = m_PostService.GetPostList(conditions);
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        FillRepeater();
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;
            postVO = m_PostService.GetPostByIdNoLazy(postVO.PostId);
            if (postVO.CloseDate.HasValue && DateTime.Now >= postVO.CloseDate.Value.AddDays(1) && m_SessionHelper.Member.MemberId.ToString().Equals(postVO.Winner))
            {
                UIHelper.SetLabelText(ref ctrl, "lblStatus", "已得標");
            }

            if (postVO.Flag == 0)
            {
                UIHelper.SetHyperLinkNUrl(ref ctrl, "aCase", "");
                UIHelper.SetHyperLinkNUrl(ref ctrl, "aEdit", string.Format("member_rent_edit.aspx?id={0}", postVO.PostId));
            }
            else
            {
                UIHelper.SetContrlVisible(ref ctrl, "aDelete", false);
                UIHelper.SetContrlVisible(ref ctrl, "aEdit", false);
                UIHelper.SetHyperLinkNUrl(ref ctrl, "aCase", string.Format("case_in.aspx?id={0}", postVO.PostId));
            }

            if (postVO.ParentPost != null)
            {
                if (postVO.ParentPost.Flag == 0)
                {
                    UIHelper.SetHyperLinkNUrl(ref ctrl, "aCase", "");
                    UIHelper.SetLabelText(ref ctrl, "lblStatus", postVO.ParentPost.GetStr_BidStatus);
                }
                else
                {
                    UIHelper.SetHyperLinkNUrl(ref ctrl, "aCase", string.Format("case_in.aspx?id={0}", postVO.ParentPost.PostId));
                }
                UIHelper.SetLabelText(ref ctrl, "lblCaseName", postVO.ParentPost.Title);
            }
        }
    }

    protected void Repeater1_ItemCommand1(object source, RepeaterCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int postId = int.Parse(e.CommandArgument.ToString());
        switch (cmdName)
        {
            case "myDelete":
                PostVO postVO = m_PostService.GetPostById(postId);
                if (postVO.Flag == 0)
                {
                    m_PostService.DeletePost(postVO);
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("刪除成功!"), false);
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("已上架的商品不能刪除!"), false);
                }
                break;
            default:
                break;
        }
        FillRepeater();
    }
}